package com.tian.auto.export;

import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.write.handler.CellWriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteTableHolder;
import com.tian.auto.StringUtils;
import com.tian.auto.info.ResData;

import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.ss.usermodel.Workbook;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Description: 拦截器，用于标记 英文或者繁体中文为空的行,如果英文或者中文为空，这行的第一个单元格进行标红处理便于 生成表格时进行塞选
 * Author:  tianjinyong
 * Date:    2019-10-30 11:22
 */
public class MarkNullCellWriteHandler implements CellWriteHandler {

    private ArrayList<ResData> resource;

    public static MarkNullCellWriteHandler create(ArrayList<ResData> resource) {
        MarkNullCellWriteHandler handler = new MarkNullCellWriteHandler();
        handler.resource = resource;

        return handler;
    }

    /**
     * 生成一个 单元格样式
     *
     * @param workbook
     * @return 获取到一个背景红色，内容居中到单元格
     */
    private CellStyle createStyle(Workbook workbook) {
        CellStyle cellStyle = workbook.createCellStyle();
        // 下边框
        cellStyle.setBorderBottom(BorderStyle.THIN);
        // 左边框
        cellStyle.setBorderLeft(BorderStyle.THIN);
        // 上边框
        cellStyle.setBorderTop(BorderStyle.THIN);
        // 右边框
        cellStyle.setBorderRight(BorderStyle.THIN);
        // 水平对齐方式
        cellStyle.setAlignment(HorizontalAlignment.CENTER);
        //cellStyle.setFillForegroundColor(IndexedColors.BLUE.getIndex());
        // 垂直对齐方式
        cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);

        cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);//设置前景填充样式
        cellStyle.setFillForegroundColor(IndexedColors.RED.getIndex());//前景填

        return cellStyle;
    }


    /**
     * 判断字符串中是否包含中文
     *
     * @param str 待校验字符串
     * @return 是否为中文
     * @warn 不能校验是否为中文标点符号
     */
    public static boolean isContainChinese(String str) {
        if (StringUtils.isEmpty(str))
            return true;

        Pattern p = Pattern.compile("[\u4e00-\u9fa5]");
        Matcher m = p.matcher(str);
        if (m.find()) {
            return true;
        }

        return false;
    }

    @Override
    public void beforeCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row, Head head, Integer columnIndex, Integer relativeRowIndex, Boolean isHead) {

    }

    @Override
    public void afterCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) {
        if (cell.getRowIndex() >= 1) {
            ResData itemData = resource.get(cell.getRowIndex() - 1);
            if (StringUtils.isEmpty(itemData.getCnValue()) || StringUtils.isEmpty(itemData.getZhrTwValue()) || isContainChinese(itemData.getEnValue())) {
                cell.getRow().getCell(0).setCellStyle(createStyle(cell.getSheet().getWorkbook()));
            }
        }
    }

    @Override
    public void afterCellDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, List<CellData> cellDataList, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) {

    }
}
